package com.hex.liyue.usermodule.presenter;

import android.text.TextUtils;

import com.hex.liyue.LiYueApplication;
import com.hex.liyue.commonmodule.listener.UploadFileListener;
import com.hex.liyue.commonmodule.manager.HttpManager;
import com.hex.liyue.commonmodule.manager.UserManager;
import com.hex.liyue.commonmodule.net.ResponseListener;
import com.hex.liyue.commonmodule.utils.OssUtils;
import com.hex.liyue.commonmodule.utils.T;
import com.hex.liyue.usermodule.bean.MyMessageResultBean;

/**
 * Created by $cmm on 2016/6/21.
 * <p>
 * 更新个人信息的业务类
 */
public class PersonEditPresenter {
    private IPersonEditView iPersonEditView;

    public PersonEditPresenter(IPersonEditView iPersonEditView) {
        this.iPersonEditView = iPersonEditView;
    }

    public void updateMyMessage() {
//        if (iPersonEditView.getIsAvater() == false) {
//            iPersonEditView.showT("请选择头像！");
//            return;
//        }
        if (TextUtils.isEmpty(iPersonEditView.getNickname())) {
            iPersonEditView.showT("请输入昵称");
            return;
        }
//        if (iPersonEditView.getSex() != 1 && iPersonEditView.getSex() != 2) {
//            iPersonEditView.showT("请选择性别");
//            return;
//        }
//        if (iPersonEditView.getBirthday() <= 0) {
//            iPersonEditView.showT("请选择正确的出生日期");
//            return;
//        }
//        if (TextUtils.isEmpty(iPersonEditView.getStarSign())) {
//            iPersonEditView.showT("请选择星座");
//            return;
//        }
//        if (TextUtils.isEmpty(iPersonEditView.getSignature())) {
//            iPersonEditView.showT("请输入个性签名");
//            return;
//        }
        iPersonEditView.showLod("加载中...");
        if (iPersonEditView.getAvatar() != null) {
            //先传头像到阿里云
            String key = OssUtils.getInstance().objectKeyUserAvater + UserManager.getUserId() + "_" + iPersonEditView.getAvatar().getName();
            OssUtils.getInstance().uploadfile(key, iPersonEditView.getAvatar().getAbsolutePath(), new UploadFileListener() {
                @Override
                public void onSucess(String path) {
                    updateUserinfo(path);
                }

                @Override
                public void onFailure() {
                    iPersonEditView.showT("上传头像失败");
                }

                @Override
                public void onProgress(long progress) {

                }
            });

        } else {
            //不更新头像
            updateUserinfo(null);
        }

    }

    private void updateUserinfo(String avater) {
        HttpManager.getInstance().
                updateMyMessage(
                        UserManager.getUserId(),
                        avater,
                        iPersonEditView.getNickname(),
                        iPersonEditView.getBirthday(),
                        iPersonEditView.getSignature(),
                        iPersonEditView.getStarSign(),
                        iPersonEditView.getSex(),
                        new ResponseListener<MyMessageResultBean>() {
                            @Override
                            public void onSuccess(MyMessageResultBean myMessageResultBean) {
                                UserManager.saveUserInfo(myMessageResultBean);
                                iPersonEditView.closeLod();
                                iPersonEditView.closeAtivity();
                                iPersonEditView.showT("成功");
                            }

                            @Override
                            public void onFailure(String responseresult) {
                                iPersonEditView.closeLod();
//                                iPersonEditView.showT(responseresult);
                                T.show(LiYueApplication.getInstance(), "陛下，你的个人信息没有修改", 0);
                            }
                        }

                );
    }

    public void getMyMessage() {
        HttpManager.getInstance().getMyMessage(UserManager.getUserId(), new ResponseListener<MyMessageResultBean>() {
                    @Override
                    public void onSuccess(MyMessageResultBean personEditPersenterBean) {
                        UserManager.saveUserInfo(personEditPersenterBean);
                        iPersonEditView.setMyMessage(personEditPersenterBean);
                    }

                    @Override
                    public void onFailure(String responseresult) {
                        iPersonEditView.showT(responseresult);
                    }
                }

        );
    }
}
